﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using SuperFox.SuperServiceReference;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.Collections.Generic;

namespace SuperFox
{

    public class WriteStatusColourConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            }

            switch ((Config.WriteStatusValues)value)
            {
                case Config.WriteStatusValues.Changed:
                    return new SolidColorBrush(Color.FromArgb(255, 175, 50, 50));
                case Config.WriteStatusValues.Saved:
                    return new SolidColorBrush(Color.FromArgb(255, 52, 176, 99));
                case Config.WriteStatusValues.Saving:
                    return new SolidColorBrush(Color.FromArgb(255, 237, 125, 63));
                case Config.WriteStatusValues.Unchanged:
                    return new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                default:
                    return new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class CostCenterTbVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)value)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class CostCenterCbVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(bool)value)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class FieldBackgroundColour : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)value)
            {
                return App.Current.Resources["REQUIREDFIELDBACKGROUND"] as SolidColorBrush;
            }
            else
            {
                return App.Current.Resources["NONREQUIREDFIELDBACKGROUND"] as SolidColorBrush;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class SelectedCostConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value as DbCostCenters).merch_costcenter;
        }
    }

    public class SelectedItemConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
             return (value as ComboBoxItem).Content;
        }
    }

    public class SelectedBranchConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value as DbBranches).merch_branchname;
        }
    }

    public class TotalPriceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (parameter.GetType() != typeof(decimal))
            {

                // decide which price to use 
                try
                {
                    int totalSelected = (int)value;

                    foreach (DbPriceTiers tier in (parameter as ObservableCollection<DbPriceTiers>))
                    {
                        if (totalSelected > tier.tier_upperrange)
                        {
                            continue;
                        }
                        else
                        {
                            return Decimal.Round(totalSelected * tier.tier_priceperitem, 2);
                        }
                    }


                    return Decimal.Round((int)value * (decimal)parameter, 2);
                }
                catch (Exception)
                {
                    return Decimal.Round((int)value * (decimal)parameter, 2);
                }
            }
            else
            {
                return Decimal.Round((int)value * (decimal)parameter, 2);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class CartImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri(Config.PRODUCT_IMAGE_DIR + "/" + value, UriKind.RelativeOrAbsolute))
            };
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class PriceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {

                return "$" + Decimal.Round((decimal)value, 2).ToString();

            }catch(Exception)
            {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class SumTotalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //DbProductOptions op = value as DbProductOptions;

            //decimal total = 0;

            //foreach (DbProductOptionsValues o in op.Values)
            //{
            //    total += o.TotalSelected * 2;
            //}

            //return total.ToString();

            ShoppingCartCollection prods = value as ShoppingCartCollection;

            if (prods == null)
                return "Total Price (Excluding GST) $0.00";

            decimal total = 0;

            foreach (DbProducts p in prods)
            {
                int count = 0;

                foreach (DbProductOptionsValues v in p.Options.Values)
                {
                    count += v.TotalSelected;
                }

                total += (p.prod_price * count);
            }

            return "Total Price (Excluding GST) $" + Decimal.Round(total, 2).ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class SumTotalGSTConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            ShoppingCartCollection prods = value as ShoppingCartCollection;

            decimal total = 0;

            int totalCount = 0;

            if (prods == null)
                return "Total Price $0.00";

            foreach (DbProducts p in prods)
            {
                int count = 0;

                foreach (DbProductOptionsValues v in p.Options.Values)
                {
                    count += v.TotalSelected;
                    totalCount += v.TotalSelected;
                }

                total += (p.prod_price * count);
            }

            if (totalCount < Config.FREE_SHIPPING_CUT_OFF)
            {
                total += 20;
            }

            return "Total Price $" + Decimal.Round(total * (decimal)1.1, 2).ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }


    public class FreightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            ShoppingCartCollection prods = value as ShoppingCartCollection;

            if (prods == null)
                return "Freight: $20";

            int totalProducts = 0;

            foreach (DbProducts p in prods)
            {
                foreach (DbProductOptionsValues v in p.Options.Values)
                {
                    totalProducts += v.TotalSelected;
                }

            }

            if (totalProducts >= Config.FREE_SHIPPING_CUT_OFF)
            {
                return "Freight: Free";
            }
            else
            {
                return "Freight: $20";
            }
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class GSTConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            ShoppingCartCollection prods = value as ShoppingCartCollection;

            decimal total = 0;

            if (prods == null)
                return "GST $0.00";

            foreach (DbProducts p in prods)
            {
                int count = 0;

                foreach (DbProductOptionsValues v in p.Options.Values)
                {
                    count += v.TotalSelected;
                }

                total += (p.prod_price * count);
            }

            return "GST $" + Decimal.Round(total * (decimal)0.1, 2).ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class PriceTotalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //DbProductOptionsValues val = value as DbProductOptionsValues;

            //return "$" + Decimal.Round(val.TotalSelected * val.Price, 2);

            return Decimal.Round((int)value * (decimal)parameter, 2).ToString();

            //DbProducts pro = value as DbProducts;

            //int total = 0;

            //foreach (DbProductOptionsValues val in pro.Options.Values)
            //{
            //    total += val.TotalSelected;
            //}

            //return "$" + Decimal.Round(total * pro.prod_price, 2).ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    //public class TotalPriceTallyConverter : IValueConverter
    //{
    //    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    //    {
    //        decimal total = 0;

    //        foreach (DbProductOptionsValues v in (value as ObservableCollection<DbProductOptionsValues>))
    //        {
    //            total += v.TotalSelected;
    //        }

    //        return Decimal.Round(total,2);
    //    }

    //    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    //    {
    //        return value;
    //    }
    //}
}
